home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group93c.txt
/
000118_icon-group-sender _Wed Dec 15 15:46:59 1993.msg
< prev
next >
Wrap
Internet Message Format
|
1994-02-02
|
3KB
Received: by cheltenham.cs.arizona.edu; Wed, 15 Dec 1993 18:26:11 MST
Message-Id: <9312152344.AA26970@enlil.premenos.sf.ca.us>
From: Ken Walker <kwalker@shara.premenos.sf.ca.us>
Subject: Re: Need help with (simple?) programming problem.
To: icon-group@cs.arizona.edu
Date: Wed, 15 Dec 93 15:46:59 PST
Mailer: Elm [revision: 66.25]
Status: R
Errors-To: icon-group-errors@cs.arizona.edu
> From Ronal F. Guilmette:
>
> Assume that I have three strings, i.e. "red ", "green ", and "blue ". Now
> I want to create a generator which will successively yield each unique
> string which contains no more than one instance of each of the original
> strings. Here are the string values which should be yielded by the various
> invocations/resumptions of the generator:
>
> ""
> "red "
> "green "
> "blue "
> "red green "
> "red blue "
> "green red "
> "green blue "
> "blue red "
> "blue green "
> "red green blue "
> "red blue green "
> "green red blue "
> "green blue red "
> "blue red green "
> "blue green red "
>
> After being invoked/resumed 16 times (and yielding each of the above strings
> once) the generator should fail. Note that the actual *order* in which the
> above strings are yielded is totally unimportant. Any order will do as long
> as I get these 16 strings (eventually).
>
> Also note that the ORDER in which the original strings appear in the genera-
> ted strings *is* significant.
I found the hard part was formulating the problem in "constructive"
terms. The problem, as I understand it, is to generate all the permutaions
of all the subsets of a list of words. Once it is formulated that way,
it is simple to solve using two recursive generators (permutaions has
certainly been done using this technique before, and I suspect subsets
has also). Here is a solution using list invocation:
procedure main()
p("red", "blue", "green")
end
procedure p(words[])
every write(permute ! (subsets ! words))
end
procedure subsets(words[])
local i, w, tail
suspend []
every i := 1 to *words do {
w := words[i : i + 1]
tail := words[i + 1 : 0]
suspend w ||| (subsets ! tail)
}
end
procedure permute(words[])
local i, head, w, tail
if *words = 0 then
return ""
every i := 1 to *words do {
head := words[1 : i]
w := words[i]
tail := words[i + 1 : 0]
suspend w || " " || (permute ! (head ||| tail))
}
end
Ken Walker, kwalker@premenos.sf.ca.us